package com.miui.theme.custom.utils

import android.annotation.SuppressLint
import android.os.Build
import android.text.TextUtils

object DeviceInfoUtils {

    /**
     * 获取设备制造商名称（如Xiaomi）
     */
    val manufacturer: String
        get() = Build.MANUFACTURER

    /**
     * 获取设备品牌名称（如Xiaomi）
     */
    val brand: String
        get() = Build.BRAND

    /**
     * 获取设备型号
     */
    val model: String
        get() = Build.MODEL

    /**
     * 获取设备产品名称
     */
    val product: String
        get() = Build.PRODUCT

    /**
     * 获取设备硬件名称
     */
    val hardware: String
        get() = Build.HARDWARE

    /**
     * 获取设备序列号（需要READ_PHONE_STATE权限）
     */
    val serial: String
        @SuppressLint("HardwareIds")
        get() = Build.SERIAL

    /**
     * 获取Android版本号（如11）
     */
    val androidVersion: String
        get() = Build.VERSION.RELEASE

    /**
     * 获取SDK版本号（如30）
     */
    val sdkVersion: Int
        get() = Build.VERSION.SDK_INT

    /**
     * 获取设备友好的显示名称（如"Xiaomi 17 Pro"）
     */
    val deviceDisplayName: String
        get() {
            return if (model.startsWith(manufacturer, ignoreCase = true)) {
                model.capitalizeWords()
            } else {
                "${manufacturer.capitalizeWords()} $model"
            }
        }

    /**
     * 检查设备是否是模拟器
     */
    val isEmulator: Boolean
        get() = (Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.startsWith("unknown")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || "google_sdk" == Build.PRODUCT)

    /**
     * 获取设备所有信息（Map格式）
     */
    val allDeviceInfo: Map<String, String>
        get() = mapOf(
            "Manufacturer" to manufacturer,
            "Brand" to brand,
            "Model" to model,
            "Product" to product,
            "Hardware" to hardware,
            "Serial" to serial,
            "AndroidVersion" to androidVersion,
            "SDKVersion" to sdkVersion.toString(),
            "DisplayName" to deviceDisplayName,
            "IsEmulator" to isEmulator.toString()
        )

    /**
     * 扩展函数：将字符串中的每个单词首字母大写
     */
    private fun String.capitalizeWords(): String {
        return if (TextUtils.isEmpty(this)) {
            this
        } else {
            split(" ").joinToString(" ") { it.capitalize() }
        }
    }
}